﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyVoteMVC.BackstageManagement.Controllers;
using MyVoteMVC.Domain;

namespace MyVoteMVC.BackstageManagement.Views
{
    public partial class DictionaryManagement : Form, IDictionaryManagementView
    {
        IDictionaryManagementController controller;
        public DictionaryManagement()
        {
            InitializeComponent();
            controller = new DictionaryManagementController(this);
        }

        #region IDictionaryManagementView Members

        public void AddDictionaryComplete(string message)
        {
            MessageBox.Show(message);
            controller.GetDictionaries();
        }

        public void ModifyDictionaryComplete(string message)
        {
            MessageBox.Show(message);
            controller.GetDictionaries();
        }

        public void DeleteDictionaryComplete(string message)
        {
            MessageBox.Show(message);
            controller.GetDictionaries();
        }

        public void GetDictionariesComplete(IList<VT_Dictionaries> dictionaries)
        {
            gvDic.DataSource = dictionaries;
        }

        public void GetByIDComplete(Domain.VT_Dictionaries dictionary)
        {
            txtName.Text = dictionary.Dictionary_Name;
            txtType.Text = dictionary.Dictionary_Type;
            txtValue.Text = dictionary.Dictionary_Value;
            numOrder.Value = dictionary.Dictionary_Order.Value;
        }

        public void OnSubmitError(string message)
        {
            MessageBox.Show(message);
        }

        #endregion

        private void DictionaryManagement_Load(object sender, EventArgs e)
        {
            controller.GetDictionaries();
        }

        private void gvDic_SelectionChanged(object sender, EventArgs e)
        {
            if (gvDic.SelectedRows.Count > 0 && gvDic.SelectedRows[0].DataBoundItem != null)
            {
                VT_Dictionaries dic = gvDic.SelectedRows[0].DataBoundItem as VT_Dictionaries;
                controller.GetByID(dic.Dictionary_ID);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            controller.AddDictionary(
                txtName.Text,
                    txtType.Text,
                    txtValue.Text,
                    (int)numOrder.Value);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {

            if (gvDic.SelectedRows.Count > 0 && gvDic.SelectedRows[0].DataBoundItem != null)
            {
                VT_Dictionaries dic = gvDic.SelectedRows[0].DataBoundItem as VT_Dictionaries;
                controller.ModifyDictionary(
                    dic.Dictionary_ID,
                    txtName.Text,
                    txtType.Text,
                    txtValue.Text,
                    (int)numOrder.Value
                    );
            }
            else
            {
                MessageBox.Show("请选择要更新的记录");
                return;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (gvDic.SelectedRows.Count > 0 && gvDic.SelectedRows[0].DataBoundItem != null)
            {
                VT_Dictionaries dic = gvDic.SelectedRows[0].DataBoundItem as VT_Dictionaries;
                controller.DeleteDictionary(dic.Dictionary_ID);
            }
            else
            {
                MessageBox.Show("请选择要删除的记录");
                return;
            }
        }
    }
}
